import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from python_ai.common.xcommon import *

spr = 1
spc = 3
spn = 0
plt.figure(figsize=[14, 6])


def my_show_img(img, title="no title", trans=None, ticks=False, **kwargs):
    global spn
    spn += 1
    plt.subplot(spr, spc, spn)
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)
    if not ticks:
        plt.axis('off')
    plt.title(title)


img = cv.imread('../../../../../large_data/pic/football.jpg')
H, W, CH = img.shape
my_show_img(img, 'ori', lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)

# cv.imshow("img", img)

hist = cv.calcHist([hsv], [0, 1], None, [180, 256], [0, 180, 0, 256])
print_numpy_ndarray_info(hist, 'hist')
my_show_img(hist, 'hist 2D', cmap='gray', ticks=True)

hist_norm = cv.normalize(hist, None, 0, 255, cv.NORM_MINMAX)
print_numpy_ndarray_info(hist_norm, 'hist_norm')
my_show_img(hist_norm, 'hist 2D norm', cmap='gray', ticks=True)

# cv.waitKey(0)
# cv.destroyAllWindows()

# plt.imshow(hist, interpolation='nearest')

plt.show()

hist = hist.astype(np.uint8)
hist = cv.cvtColor(hist, cv.COLOR_GRAY2BGR)
hist = cv.resize(hist, (W, H), interpolation=cv.INTER_NEAREST)
hist_norm = hist_norm.astype(np.uint8)
hist_norm = cv.cvtColor(hist_norm, cv.COLOR_GRAY2BGR)
hist_norm = cv.resize(hist_norm, (W, H), interpolation=cv.INTER_NEAREST)
img_all = np.concatenate((img, hist, hist_norm),
                         axis=1)
cv.imshow('img all', img_all)
